package com.wulian.user.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.wulian.common.vo.ResultMoudel;
import com.wulian.user.dto.PostUserDto;
import com.wulian.user.entity.PostUser;
import com.wulian.user.mapper.PostUserMapper;
import com.wulian.user.service.PostUserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 *@Description:()
 * @author  caifulin
 * @date  2020/10/28 14:57
 * @version 1.0
 */
@Service
@Slf4j
@Transactional
public class PostUserServiceImpl extends ServiceImpl<PostUserMapper, PostUser> implements PostUserService {
    @Autowired
    private PostUserMapper postUserMapper;

    @Override
    public ResultMoudel updatePostUser(PostUserDto postUserDto,Integer userId) {
        Date newDate = new Date();
        PostUser param = new PostUser();
        param.setDelFlag(1);
        param.setUpdateTime(newDate);
        param.setUpdateId(userId);
        PostUser where  = new PostUser();
        where.setCompanyId(postUserDto.getCompanyId());
        where.setPostId(postUserDto.getPostId());
        where.setDelFlag(0);
        EntityWrapper<PostUser> postUserEntityWrapper = new EntityWrapper(where);
        postUserMapper.update(param , postUserEntityWrapper);
        List<Integer> userIdList = postUserDto.getUserIdList();
        int flag = 0;
        for(Integer userIds: userIdList){
            PostUser ins = new PostUser();
            ins.setCreateId(userId);
            ins.setCreateTime(newDate);
            ins.setDelFlag(0);
            ins.setUpdateTime(newDate);
            ins.setCompanyId(postUserDto.getCompanyId());
            ins.setPostId(postUserDto.getPostId());
            ins.setUserId(userIds);
            ins.setUpdateId(userId);
            flag  += postUserMapper.insert(ins);
        }
        if(flag == userIdList.size()){
            return  new ResultMoudel().success("操作成功");
        }
        return new ResultMoudel().error("操作失败");
    }
}